package com.budweiser.oauth.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestControllerAdvice
public class ExceptionHandlerAdvice {

	@ExceptionHandler({AuthenticationCredentialsNotFoundException.class})
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public Map<String, Object> userBadReuqestException(Throwable throwable) {
		log.error("用户登录异常", throwable);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
		data.put("message", "用户登录异常，请联系管理员");
		return data;
	}

	@ExceptionHandler({Exception.class})
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public Map<String, Object> badReuqestException(Throwable throwable) {
		log.error("登录异常", throwable);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
		data.put("message", "登录异常，请联系管理员");
		return data;
	}
}
